package com.hzmg.web.creater.factory;


import com.hzmg.web.annotation.Unless;
import com.hzmg.web.creater.strategy.RouterStrategy;
import com.hzmg.web.utils.SpringUtils;
import io.vertx.ext.web.RoutingContext;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 请求参数解析方式工厂
 * @author zbw
 */
@Component
public class RequestFactory implements ApplicationContextAware {

    private Map<Class<? extends Annotation>, RouterStrategy> map = new ConcurrentHashMap<>();
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        Map<String, RouterStrategy> tempMap = applicationContext.getBeansOfType(RouterStrategy.class);
        tempMap.values().forEach(routerStrategy -> map.put(routerStrategy.getType(), routerStrategy));
    }

    public void dealRouter(List<Class<? extends Annotation>> parameterAnnotations, RoutingContext ctx, Method method, List<Object> parameters, int i) {
        Class<? extends Annotation> unless=SpringUtils.getBean(Unless.class).annotationType();
        map.get(parameterAnnotations.stream()
                .filter(parameterAnnotation -> map.get(parameterAnnotation) != null)
                .findAny().orElse(unless))
                .dealRouter(ctx, method, parameters, i);
    }


}
